using System;
using System.Collections.Generic;
using System.Text;
using Entidades;
using System.Data;


namespace Jetaime.Genero
{
    class GeneroController
    {
        #region PROPIEDADES
        private Jetaime.GeneroView gView;
        private GeneroE genero;
        private GeneroModel gModel;
        private String ABM = String.Empty;
        #endregion        
        
        #region CONSTRUCTORES
        public GeneroController(String ABM, GeneroE genero): this(ABM)
        {   
            this.genero = genero;                 
            gView.txtDescripcion.Text = this.genero.Descripcion;
        }

        public GeneroController(String ABM)
        {
            this.gView = new GeneroView();
            this.gModel = new GeneroModel();
            this.ABM = ABM;
            this.InitEvent();    
        }
        #endregion
        
        #region METODOS
        private void InitEvent()
        {
            gView.Load += new EventHandler(gView_Load);
            gView.btnAceptar.Click += new EventHandler(btnAceptar_Click);
            gView.btnCancelar.Click += new EventHandler(btnCancelar_Click);
            gView.txtDescripcion.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtDescripcion_KeyPress);
        }

        void gView_Load(object sender, EventArgs e)
        {
            gView.txtDescripcion.Focus();
        }

        void txtDescripcion_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnAceptar_Click(null, null);
            }
        }

        void btnCancelar_Click(object sender, EventArgs e)
        {
            gView.Close();
        }

        void btnAceptar_Click(object sender, EventArgs e)
        {   
            if(this.ABM == "A")
            {
                GeneroE genero = new GeneroE();
                genero.Descripcion = this.gView.txtDescripcion.Text;
                this.gModel.Guardar("A", genero);                 
            }
            else if(this.ABM == "M")
            {
                this.genero.Descripcion = gView.txtDescripcion.Text;
                gModel.Guardar("M", this.genero);
            }
            this.gView.Close();
        }
        
        public void Show()
        {
            gView.ShowDialog();
        }
        
        private void CargarTextBox(GeneroE genero)
        {
            gView.txtDescripcion.Text = genero.Descripcion;
        }
        #endregion            
    }
}
